﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// 调整组件的大小
    /// </summary>
    public class ResizeOperation : OperationBase
    {
        private DesignView _designView;
        private Coolzon.UI.Part _partInstance;

        private double _originalLeft;
        private double _originalTop;
        private double _originalWidth;
        private double _originalHeight;

        private double _newLeft;
        private double _newTop;
        private double _newWidth;
        private double _newHeight;

        public ResizeOperation(DesignView designView, Coolzon.UI.Part partInstance, double newLeft, double newTop, double newWidth, double newHeight)
        {
            _designView = designView;
            this._partInstance = partInstance;

            this._originalLeft = partInstance.XLeft;
            this._originalTop = partInstance.XTop;
            this._originalWidth = partInstance.XWidth;
            this._originalHeight = partInstance.XHeight;

            this._newLeft = newLeft;
            this._newTop = newTop;
            this._newWidth = newWidth;
            this._newHeight = newHeight;
        }

        protected override void OnDo()
        {
            try
            {
                _partInstance.XWidth = _newWidth;
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Width", typeof(double), _newWidth);
                _partInstance.XHeight = _newHeight;
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Height", typeof(double), _newHeight);
                //如果是场景则重新打开，且不改变Left，Top
                if (_partInstance is Coolzon.UI.ScenePart)
                {
                    _designView.API_ReOpenScene();
                }
                else
                {
                    _partInstance.XLeft = _newLeft;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Left", typeof(double), _newLeft);
                    _partInstance.XTop = _newTop;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Top", typeof(double), _newTop);
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(Library.LogTAG, "更新组件{0}大小宽高属性出错,ex:{1}", _partInstance.Name, ex);
            }
        }

        protected override void OnUndo()
        {
            try
            {
                _partInstance.XWidth = _originalWidth;
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Width", typeof(double), _originalWidth);
                _partInstance.XHeight = _originalHeight;
                Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Height", typeof(double), _originalHeight);
                if (_partInstance is Coolzon.UI.ScenePart)
                {
                    _designView.API_ReOpenScene();
                }
                else
                {
                    _partInstance.XLeft = _originalLeft;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Left", typeof(double), _originalLeft);
                    _partInstance.XTop = _originalTop;
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(_partInstance.Metadata, "Top", typeof(double), _originalTop);
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(Library.LogTAG, "撤销组件{0}大小宽高属性出错,ex:{1}", _partInstance.Name, ex);
            }
        }
    }
}
